

package com.yf.tz.service.impl;

import com.yf.response.RRException;
import com.yf.tz.enums.ServerConfigStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.yf.tz.vo.down.ServerConfigDown;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import com.yf.tz.vo.down.ServerConfigDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import com.yf.tz.dao.ServerConfigDao;
import com.yf.tz.entity.ServerConfigEntity;
import com.yf.tz.vo.ServerConfigVo;
import com.yf.tz.service.ServerConfigService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Aug 10 10:29:20 CST 2020
 */
@Service
public class ServerConfigServiceImpl extends BaseServiceImpl<ServerConfigEntity> implements ServerConfigService {

    private ServerConfigDao dao;


    @Autowired
    public void setDao(ServerConfigDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<ServerConfigVo> findVo(ServerConfigVo vo){
        return dao.findVo(vo);
    }

    @Override
    public ServerConfigVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    public int saveVo(ServerConfigVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    public int updateVo(ServerConfigVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<ServerConfigVo> list) {
        try {
            List<ServerConfigDown> datas = list.stream().map(item -> new ServerConfigDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), ServerConfigDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    /**内部方法**/



    /**外部方法实现**/

    @Override
    public ServerConfigEntity findConfig() {
        List<ServerConfigEntity> lists = dao.find(null);
        if(lists.size()==0){
            throw new RRException("没有配置");
        }
        ServerConfigEntity configEntity = lists.get(0);
        for(ServerConfigEntity item:lists){
            if(item.getStatus().equals(ServerConfigStatusEnum.OPEN.getIndex())){
                configEntity = item;
            }
        }
        return configEntity;
    }
}
